<?php

class Tablet_FavoritesController extends Zend_Controller_Action
{

    public function init()
    {
        Zend_Session::start();
        $Session = new Zend_Session_Namespace ('Params'); 
        $this->view->Persisted = $Session->Persisted;
        $this->_redirector = $this->_helper->getHelper('Redirector');   
        $this->_helper->layout->setLayout('ios');
        $this->view->links = array ( 
                                     array ('text'=>'Home', 'href'=>'/tablet/index/index') 
                              );
        $this->view->anchor_left = array ('text'=>'Back',
                                            'href'=>$this->view->links[0]['href']
                                             );
    }

    public function indexAction()
    {
        $request    = $this->getRequest();  
        $pagenumber = $request->getParam('page'); 
        $this->view->links[] = array ('text'=>'Bookmarks', 'active'=>true) ;
        $m = new Default_Model_BookmarkManager;
        $m->Collate(); 
        $startIndex = ($pagenumber - 1) * 10;
        $this->view->groups    = array_slice ($m->Collated, $startIndex, 10);
        $this->view->page      = $pagenumber;
        $this->view->size      = sizeof($m->Collated); 
        $this->view->links[] = array ('text'=>'Media', 'href'=>'/tablet/media/');
    }

    public function listAction()
    {
        $request    = $this->getRequest(); 
        $groupname  = $request->getParam('group'); 
        $pagenumber = $request->getParam('at'); 
        $pageitem   = $request->getParam('item'); 
        if (isset ($pageitem)) { 
#            $target      = "/favorites/load/group/{$groupname}/page/{$pagenumber}/article/{$index}/index/{$id}";
            $target      = $this->view->url( array(  "module"     => "default", 
                                                       "controller" => "favorites", 
                                                       "action"     => "load", 
                                                       "group"      => $groupname, 
                                                       "key"        => $pageitem ) );

            $destination = $this->view->url( array(  "action"=>"view", "item" =>$pageitem   ) );
            $this->_helper->getHelper("FlashMessenger")->addMessage($destination);
            $this->_redirector
                 ->gotoUrl($target); 
            return;
        }
        if (!isset($pagenumber)) $pagenumber = 1;
        $startIndex = ($pagenumber - 1) * 10;
        $this->view->links[] = array ('text'=>'Bookmarks', 'href'=>$this->view->url(array('action'=>'index'))) ;
        $m = new Default_Model_BookmarkManager;
        $m->Collate();  
        $this->view->items     = array_slice ($m->Collated[$groupname], $startIndex, 10);#$m->Collated[$groupname];
        $this->view->page      = $pagenumber;
        $this->view->group     = $groupname;
        $this->view->size      = sizeof($m->Collated[$groupname]); 
        $this->view->links[]   = array ('text'=>$groupname, 'info'=> sizeof($m->Collated[$groupname]), 'active'=>true ) ;
        $this->view->links[]   = array ('text'=>'Media', 'href'=>'/tablet/media/');
        $this->view->anchor_left = array ('text'=>'Back','href'=>$this->view->url(array('action'=>'index'))) ;
    }

    public function viewAction()
    {
        $request    = $this->getRequest();  
        $item       = $request->getParam('item'); 
        $pagenumber = $request->getParam('at');  
        $groupname  = $request->getParam('group');  
 
        $b = new Default_Model_BookmarkManager;
        $bookmark = $b->Find ($groupname, $item);

        $this->view->links[] = array ('text'=>'Bookmarks', 'href'=>$this->view->url(array('action'=>'index'))) ;
        $this->view->links[] = array ('text'=>$groupname, 'href'=>$this->view->url(array('action'=>'list', 'item'=>NULL))) ; 
        $this->view->anchor_left = array ('text'=>'Back','href'=>$this->view->url(array('action'=>'list', 'item'=>NULL))) ; 

        $DATA_URI  = DATA_PATH . "/bookmark/{$bookmark->Path}";
        $bookmark->Populate ($DATA_URI);
        $this->view->page = $pagenumber;
        $this->view->subject = $bookmark->Name;
        $this->view->items   = $bookmark->Items;
        $this->view->group   = $bookmark->Group;
        $this->view->key     = $key;
        $this->view->item    = $item;
        $this->view->links[] = array ('text'=>$bookmark->Name, 'active'=>true, 'info'=>sizeof($bookmark->Items)) ; 
        $this->view->links[] = array ('text'=>'Media', 'href'=>'/tablet/media/');
        $this->view->anchor_right = array ('text'=>'Play',
                                             'href'=>"javascript:ArticlePlay('anchor_right')"
                                             );
    }


}





